package servlets;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取指定名称的请求参数值
        //getParameter("name")方法，本质上等同于getParameterValues("name")[0]
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String name1 = req.getParameterValues("name")[0];

        System.out.println("name = " + name);
        System.out.println("age = " + age);



        // 获取所有请求参数名称
        Enumeration<String> names = req.getParameterNames();
        // 遍历枚举
        while(names.hasMoreElements()) {
            String eleName = names.nextElement();
            String eleValue = req.getParameter(eleName);
            System.out.println(eleName + " ==== " + eleValue);
        }

        // 获取指定参数的所有值
        String[] hobby = req.getParameterValues("hobby");
        for (String h : hobby) {
            System.out.println(h);
        }

        // 获取存放请求参数的Map
        Map<String, String[]> map = req.getParameterMap();
        for (String key : map.keySet()) {
            System.out.println(key + " ------ " + req.getParameter(key));
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
